package com.qiguliuxing.dts.wx.config;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Configuration
@EnableSwagger2
@ConditionalOnExpression("true")
@ComponentScan("com.qiguliuxing.dts.wx.controller")
public class SwaggerConfig {
	private final Log logger = LogFactory.getLog(SwaggerConfig.class);

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.groupName("小程序接口模块")
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.qiguliuxing.dts.wx.controller"))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.paths(PathSelectors.any())
				.build()
				.securitySchemes(security());
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("聚惠星商城-小程序接口模块在线接口文档")
				.description("此文档仅供开发人员使用")
				.termsOfServiceUrl("http://localhost:8080/")
				.contact(new Contact("dts-shop","http://localhost:8080","juhuixing@aliyun.com"))
				.version("2.0")
				.build();
	}

	private List<SecurityScheme> security() {
		ApiKey apiKey = new ApiKey("认证token", "X-Dts-Token", "header");
		return Collections.singletonList(apiKey);
	}

}